package com.spring.cloud.zuul.filter;

import javax.servlet.http.HttpServletRequest;

import com.netflix.zuul.ZuulFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.context.RequestContext;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

@Component
public class MyZuulFilter extends ZuulFilter {

	private static Logger log = LoggerFactory.getLogger(MyZuulFilter.class);

	/**
	 * 前置过滤器
	 * pre：可以在请求被路由之前调用
	 * route：在路由请求时候被调用
	 * post：在route和error过滤器之后被调用
	 * error：处理请求时发生错误时被调用
	 * */
	@Override
	public String filterType() {
		log.info("========>ZuulFilter type pre");
		return PRE_TYPE;
	}

	/**优先级为0，数字越大，优先级越低*/
	@Override
	public int filterOrder() {
		return 0;
	}
	/**是否执行该过滤器，此处为true，说明需要过滤  自行 run*/
	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		log.info("========ZuulFilter：function:{} path:{}", request.getMethod(), request.getRequestURL().toString());
		ctx.setSendZuulResponse(true);
		ctx.setResponseStatusCode(200);
		ctx.set("isSuccess", true);
		return null;

	}


}
